//
//  MyTouchIdDemo.m
//  QiYReader
//
//  Created by 奇阅科技 on 14/12/20.
//  Copyright (c) 2014年 奇阅科技. All rights reserved.
//

#import "MyTouchIdDemo.h"
#import "Config.h"
#import <LocalAuthentication/LAContext.h>
#import <LocalAuthentication/LAError.h>
@implementation MyTouchIdDemo
-(void)viewDidLoad{
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = NO;
    [self setBarBackgroundImage:@"navBackground"];
    [self setTitleForNavigation:@"充值"];
    [self setLeftBackButtonForNavigation];
    self.view.backgroundColor = [UIColor whiteColor];
    LAContext *myContext = [[LAContext alloc] init];
    NSError *authError = nil;
    NSString *myLocalizedReasonString = @"Restricted Area!";
    if (!myContext) {
        return;
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
    if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {
        [myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
                  localizedReason:myLocalizedReasonString
                            reply:^(BOOL success, NSError *error) {
                                if (success) {
                                    // User authenticated successfully, take appropriate action
                                    alert.title = @"Success";
                                    alert.message = @"You have access to private content!";
                                    [alert show];
                                } else {
                                    // User did not authenticate successfully, look at error and take appropriate action
                                    alert.title = @"Fail";
                                    switch (error.code) {
                                        case LAErrorUserCancel:
                                            alert.message = @"Authentication Cancelled";
                                            break;
                                            
                                        case LAErrorAuthenticationFailed:
                                            alert.message = @"Authentication Failed";
                                            break;
                                            
                                        case LAErrorPasscodeNotSet:
                                            alert.message = @"Passcode is not set";
                                            break;
                                            
                                        case LAErrorSystemCancel:
                                            alert.message = @"System cancelled authentication";
                                            break;
                                            
                                        case LAErrorUserFallback:
                                            alert.message = @"You chosed to try password";
                                            break;
                                            
                                        default:
                                            alert.message = @"You cannot access to private content!";
                                            break;
                                    }
                                    [alert show];
                                }
                            }];
    }else {
        // Could not evaluate policy; look at authError and present an appropriate message to user
        alert.title = @"Warning";
        
        if(authError.code == LAErrorTouchIDNotEnrolled) {
            alert.message = @"You do not have any fingerprints enrolled!";
        }else if(authError.code == LAErrorTouchIDNotAvailable) {
            alert.message = @"Your device does not support TouchID authentication!";
        }else if(authError.code == LAErrorPasscodeNotSet){
            alert.message = @"Your passcode has not been set";
        }
        
        [alert show];
    }

}
@end
